var the_shopping_cart = new Vue({
		el: "#app",
		data() {
			return {
				allChecked: false, //全选状态  true|false
				total: 0, //总价
				cartList: [{
						img: '../../template/static/img/1.jpg',
						text: '准上人关热与相青音',
						text1: '标准装:3盒装',
						text2: '3.5',
						id: 0,
						num: 1
					},
					{
						img: '../../template/static/img/1.jpg',
						text: '准上人关热与相青音',
						text1: '标准装:3盒装',
						text2: '1.22',
						id: 1,
						num: 1
					},
					{
						img: '../../template/static/img/1.jpg',
						text: '准上人关热与相青音',
						text1: '标准装:3盒装',
						text2: '2.1',
						id: 2,
						num: 1
					}
				]
			}
		},
		//计算属性
		computed: {},
		//监听属性
		watch: {},
		//钩子函数
		created() {
			// console.log(this.cartList);
			this.cartList.map((item, index) => {
				this.$set(item, 'checked', true);
				// js
				this.allChecked = true;
				return item;
			})
			this.calcTotal() //默认调用计算总价方法
		},
		mounted() {

		},
		methods: {
			//计算总价方法
			calcTotal() {
				let list = this.cartList; //赋值给list方便操作
				if (list.length === 0) {
					return;
				}
				let total = 0;
				let checked = true;
				list.forEach(item => {
					if (item.checked === true) {
						total += item.text2 * item.num; //每个商品的价格乘以个数
					} else if (checked === true) {
						checked = false;
					}
				})
				this.allChecked = checked;
				this.total = Number(total.toFixed(2)); //总价
			},
			//单选
			changeitem(item) {
				item.checked = !item.checked
				if (!item.checked) {
					this.allChecked = false
				} else {
					//用every方法检测数组cartList的所有元素是否都是true
					const cartList2 = this.cartList.every(item => {
						return item.checked === true
					})
					if (cartList2) {
						this.allChecked = true
					} else {
						this.allChecked = false
					}
				}
				this.calcTotal() //点击调用计算总价方法
			},
			//全选，全不选
			selectAll() {
				this.allChecked = !this.allChecked
				if (this.allChecked) {
					this.cartList.map(item => {
						item.checked = true
					})
				} else {
					this.cartList.map(item => {
						item.checked = false
					})
				}
				this.calcTotal() //点击调用计算总价方法
			},
			click_settlement() { //跳转结算页面
				window.location.href = "settlement.html"
			}
		}
	})
